<!DOCTYPE html>
<html>
	<head>
		<title>testing dom-contruct.place</title>
		<script src="../dojo.js" data-dojo-config="isDebug:true"></script>
		<script>
			require(["doh", "dojo/dom-construct", "dojo/dom", "dojo/domReady!"], function(doh, construct, dom){

				var TEST_POSITION = 2;
				var lastHtml = "<div id='last'><h1>First</h1></div>";
				var firstHtml = "<div id='first'><h1>First</h1></div>";
				var beforeHtml = "<div id='before'></div>";
				var afterHtml = "<div id='after'></div>";
				var replaceHtml = "<div id='replace'></div>";
				var onlyHtml = "<div id='only'><h1>first</h1></div>";

				var posHtml = "<div id='pos'><div>first</div><div>second</div><div>last</div></div>";

				var HTMLString = "<div id=\"test\">Test</div>";

				var nodes = {};
				var child;
				var fragment;

				function clearTarget() {
					document.body.innerHTML = "";
					child = construct.toDom(HTMLString);
					nodes.last = construct.toDom(lastHtml);
					nodes.first = construct.toDom(firstHtml);
					nodes.before = construct.toDom(beforeHtml);
					nodes.after = construct.toDom(afterHtml);
					nodes.replace = construct.toDom(replaceHtml);
					nodes.only = construct.toDom(onlyHtml);
					nodes.pos = construct.toDom(posHtml);
					document.body.appendChild(nodes.last);
					document.body.appendChild(nodes.first);
					document.body.appendChild(nodes.before);
					document.body.appendChild(nodes.after);
					document.body.appendChild(nodes.replace);
					document.body.appendChild(nodes.only);
					document.body.appendChild(nodes.pos);
					fragment = document.createDocumentFragment();
					fragment.appendChild(document.createElement("div"));
					fragment.appendChild(document.createElement("div"));
					fragment.appendChild(document.createElement("div"));
				}

				function elementsEqual(elementA, elementB) {
					return elementA.id === elementB.id &&
						elementA.tagName === elementB.tagName &&
						elementA.innerHTML === elementB.innerHTML;
				}

				doh.register([
					{
						setUp: clearTarget,
						name: "last - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.last);
							doh.assertTrue(elementsEqual(child, nodes.last.lastChild));
						}
					},{
						setUp: clearTarget,
						name: "last - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "last");
							doh.assertTrue(elementsEqual(child, nodes.last.lastChild));
						}
					},{
						setUp: clearTarget,
						name: "last - place html string with fragment reference",
						runTest: function(t){
							construct.place(HTMLString, fragment);
							doh.assertTrue(elementsEqual(child, fragment.lastChild));
						}
					},{
						setUp: clearTarget,
						name: "last - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.last);
							doh.assertEqual(child, nodes.last.lastChild);
						}
					},{
						setUp: clearTarget,
						name: "last - place node with id reference",
						runTest: function(t){
							construct.place(child, "last");
							doh.assertEqual(child, nodes.last.lastChild);
						}
					},{
						setUp: clearTarget,
						name: "last - place node with fragment reference",
						runTest: function(t){
							construct.place(child, fragment);
							doh.assertEqual(child, fragment.lastChild);
						}
					},{
						setUp: clearTarget,
						name: "first - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.first, "first");
							doh.assertTrue(elementsEqual(child, nodes.first.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "first - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "first", "first");
							doh.assertTrue(elementsEqual(child, nodes.first.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "first - place html string with fragment reference",
						runTest: function(t){
							construct.place(HTMLString, fragment, "first");
							doh.assertTrue(elementsEqual(child, fragment.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "first - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.first, "first");
							doh.assertEqual(child, nodes.first.firstChild);
						}
					},{
						setUp: clearTarget,
						name: "first - place node with id reference",
						runTest: function(t){
							construct.place(child, "first", "first");
							doh.assertEqual(child, nodes.first.firstChild);
						}
					},{
						setUp: clearTarget,
						name: "first - place node with fragment reference",
						runTest: function(t){
							construct.place(child, fragment, "first");
							doh.assertEqual(child, fragment.firstChild);
						}
					},{
						setUp: clearTarget,
						name: "before - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.before, "before");
							doh.assertTrue(elementsEqual(child, nodes.before.previousSibling));
						}
					},{
						setUp: clearTarget,
						name: "before - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "before", "before");
							doh.assertTrue(elementsEqual(child, nodes.before.previousSibling));
						}
					},{
						setUp: clearTarget,
						name: "before - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.before, "before");
							doh.assertTrue(child, nodes.before.previousSibling);
						}
					},{
						setUp: clearTarget,
						name: "before - place node with id reference",
						runTest: function(t){
							construct.place(child, "before", "before");
							doh.assertEqual(child, nodes.before.previousSibling);
						}
					},{
						setUp: clearTarget,
						name: "after - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.after, "after");
							doh.assertTrue(elementsEqual(child, nodes.after.nextSibling));
						}
					},{
						setUp: clearTarget,
						name: "after - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "after", "after");
							doh.assertTrue(elementsEqual(child, nodes.after.nextSibling));
						}
					},{
						setUp: clearTarget,
						name: "after - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.after, "after");
							doh.assertEqual(child, nodes.after.nextSibling);
						}
					},{
						setUp: clearTarget,
						name: "after - place node with id reference",
						runTest: function(t){
							construct.place(child, "after", "after");
							doh.assertEqual(child, nodes.after.nextSibling);
						}
					},{
						setUp: clearTarget,
						name: "replace - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.replace, "replace");
							doh.assertEqual(document.getElementById("replace"), undefined);
							doh.assertTrue(elementsEqual(child, document.getElementById('test')));
						}
					},{
						setUp: clearTarget,
						name: "replace - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "replace", "replace");
							doh.assertEqual(document.getElementById("replace"), undefined);
							doh.assertTrue(elementsEqual(child, document.getElementById('test')));
						}
					},{
						setUp: clearTarget,
						name: "replace - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.replace, "replace");
							doh.assertEqual(document.getElementById("replace"), undefined);
							doh.assertEqual(child, document.getElementById('test'));
						}
					},{
						setUp: clearTarget,
						name: "replace - place node with id reference",
						runTest: function(t){
							construct.place(child, "replace", "replace");
							doh.assertEqual(document.getElementById("replace"), undefined);
							doh.assertEqual(child, document.getElementById('test'));
						}
					},{
						setUp: clearTarget,
						name: "only - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.only, "only");
							doh.assertEqual(nodes.only.children.length, 1);
							doh.assertTrue(elementsEqual(child, nodes.only.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "only - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "only", "only");
							doh.assertEqual(nodes.only.children.length, 1);
							doh.assertTrue(elementsEqual(child, nodes.only.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "only - place html string with fragment reference",
						runTest: function(t){
							construct.place(HTMLString, fragment, "only");
							doh.assertEqual(fragment.childNodes.length, 1);
							doh.assertTrue(elementsEqual(child, fragment.firstChild));
						}
					},{
						setUp: clearTarget,
						name: "only - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.only, "only");
							doh.assertEqual(child, nodes.only.firstChild);
							doh.assertEqual(1, nodes.only.children.length);
						}
					},{
						setUp: clearTarget,
						name: "only - place node with id reference",
						runTest: function(t){
							construct.place(child, "only", "only");
							doh.assertEqual(child, nodes.only.firstChild);
							doh.assertEqual(1, nodes.only.children.length);
						}
					},{
						setUp: clearTarget,
						name: "only - place node with fragment reference",
						runTest: function(t){
							construct.place(child, fragment, "only");
							doh.assertEqual(fragment.childNodes.length, 1);
							doh.assertEqual(child, fragment.firstChild);
						}
					},{
						setUp: clearTarget,
						name: "pos - place html string with node reference",
						runTest: function(t){
							construct.place(HTMLString, nodes.pos, TEST_POSITION);
							doh.assertTrue(elementsEqual(child, nodes.pos.children[TEST_POSITION]));
						}
					},{
						setUp: clearTarget,
						name: "pos - place html string with id reference",
						runTest: function(t){
							construct.place(HTMLString, "pos", TEST_POSITION);
							doh.assertTrue(elementsEqual(child, nodes.pos.children[TEST_POSITION]));
						}
					},{
						setUp: clearTarget,
						name: "pos - place html string with fragment reference",
						runTest: function(t){
							construct.place(HTMLString, fragment, TEST_POSITION);
							doh.assertTrue(elementsEqual(child, fragment.childNodes[TEST_POSITION]));
						}
					},{
						setUp: clearTarget,
						name: "pos - place node with node reference",
						runTest: function(t){
							construct.place(child, nodes.pos, TEST_POSITION);
							doh.assertEqual(child, nodes.pos.children[TEST_POSITION]);
						}
					},{
						setUp: clearTarget,
						name: "pos - place node with id reference",
						runTest: function(t){
							construct.place(child, "pos", TEST_POSITION);
							doh.assertEqual(child, nodes.pos.children[TEST_POSITION]);
						}
					},{
						setUp: clearTarget,
						name: "pos - place node with fragment reference",
						runTest: function(t){
							construct.place(child, fragment, TEST_POSITION);
							doh.assertEqual(child, fragment.childNodes[TEST_POSITION]);
						}
					}

				]);

				doh.run();
			});
		</script>
	</head>
	<body>
		<div id="target"></div>
	</body>
</html>
